package com.leetcode.partition3;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/8/7 10:31
 */
public class LC279完全平方数 {

    public static int numSquares(int n) {
        return dynamicProgramming(n);
    }

    public static void main(String[] args) {
        System.out.println(numSquares(5));
    }

    private static int dynamicProgramming(int n) {
        //dp[i]：完全平方数之和等于i的最小完全平方数的个数
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 1; j * j <= i; j++) {
                //dp[i]可以由dp[i - j * j]推出，加上j * j这个完全平方数，就是dp[i - j * j] + 1
                dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
            }
        }
        System.out.println(Arrays.toString(dp));
        return dp[n];
    }
}
